package com.zy.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.constant.ProductConstants;
import com.zy.domain.Category;
import com.zy.ex.handler.BusinessException;
import com.zy.service.CategoryService;
import com.zy.mapper.CategoryMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author RASH_ZY
* @description 针对表【category(商品类目)】的数据库操作Service实现
* @createDate 2025-04-02 11:54:04
*/
@Service
@CacheConfig(cacheNames = "com.zy.service.impl.CategoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询系统所有商品类目，全量查询，写入缓存
     * @return
     */
    @Override
    @Cacheable(key = ProductConstants.ALL_CATEGORY_LIST_KEY)
    public List<Category> queryAllCategoryList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .orderByDesc(Category::getSeq)
        );
    }

    /**
     * 查询系统商品一级类目，全量查询，写入缓存
     * 查询条件：parentId = 0L, status = 1
     * @return
     */
    @Override
    @Cacheable(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    public List<Category> queryFirstCategoryList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId,0)
                .eq(Category::getStatus,1)
                .orderByDesc(Category::getSeq)
        );
    }

    /**
     * 新增商品类目
     * @param category
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.WX_FIRST_CATEGORY)
    })
    public Boolean saveCategory(Category category) {
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        return categoryMapper.insert(category)>0;
    }

    /**
     * 修改商品类目信息
     *  允许：
     *      二级类目转换为一级类目
     *      没有子类目的一级类目转换为二级类目
     * @param category
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.WX_FIRST_CATEGORY)
    })
    public Boolean modifyCategory(Category category) {
        //获取修改之后的parentId，即前端传来的parentId
        Long parentId = category.getParentId();

        //获取修改之前类目的parentId
        Category beforeCategory = categoryMapper.selectById(category.getCategoryId());
        Long beforeParentId = beforeCategory.getParentId();

        //一级类目 -> 二级类目 修改之前parentId=0, 修改时前端传参parentId!=0
        if (beforeParentId == 0L && parentId != 0L &&  parentId != null) {
            //获取修改之前类目的子类目信息
            //条件：数据表中的parentId = 修改前的categoryId
            List<Category> childList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                    .eq(Category::getParentId, beforeCategory.getCategoryId())
            );
            //判断子类目是否有值
            if (CollectionUtil.isNotEmpty(childList) && childList.size() != 0) {
                //存在子类目
                throw new BusinessException("当前类目存在子类目，不可修改");
            }
        }

        //二级类目 -> 一级类目 修改之前parentId!=0, 修改时前端传参parentId=null
        if (beforeParentId != 0L && parentId == null) {
            category.setParentId(0L);
        }
        return categoryMapper.updateById(category)>0;
    }

    /**
     * 删除商品类目
     *  1.存在子类目的一级类目不可删除
     *  2.其他的可直接删除
     * @param categoryId
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.WX_FIRST_CATEGORY)
    })
    public Boolean removeCategoryById(Long categoryId) {
        //查询当前类目的子类目集合
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, categoryId)
        );
        //判断子类目是否有值
        if (CollectionUtil.isNotEmpty(categoryList) && categoryList.size() != 0) {
            //存在子类目，不可删除
            throw new BusinessException("当前类目存在子类目，不可删除");
        }
        //不存在子类目，删除
        return categoryMapper.deleteById(categoryId)>0;
    }

}




